package Concurrency2.Chapter04_Visibility_Orderliness;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Instruction：
 * Author：@author MaLi
 */
public class T02_Visibility {

    //共享变量作为开关, 不加volatile, 线程1的while循环停不掉
    private static /*volatile*/ boolean flag = true;

    public static void main(String[] args) throws IOException {
        ExecutorService service = Executors.newFixedThreadPool(2);
        service.submit(()->{
            while (flag) {
            }
            System.out.println("循环Stoped");
        });

        service.submit(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            flag = false;
            System.err.println("关闭循环");
        });
        System.in.read();

    }
}
